unit UntSaleObj;

interface

uses
  DBClient;

type
  TSaleObj = class(TObject)
  public
    function insertSaleDT(cdsSaleDT: TClientDataSet; hdid: Integer;isin:Integer;
        cdsDouDis: TClientDataSet): Boolean;
    function InsertSaleHD(salesmen, vipCode, date, saleNO, Storecode: string;
        totalQty: Integer; totalAmount, card, cash, creditCard, shoppingCard,
        otherCard, actualAmount: Double; isin: Integer; paymentdesc: string;
        errorMoney: Double): Integer;
  end;

implementation

uses
  UntDMDB, Variants, UntStoreGoodsInfoDB;

function TSaleObj.insertSaleDT(cdsSaleDT: TClientDataSet; hdid: Integer;
    isin:Integer;cdsDouDis: TClientDataSet): Boolean;
var sqlStr : string;
begin
  sqlStr := ' insert into saledt(hdid,goodsID,qty,salePrice,actualPrice,discount,amount,`desc`,isin,dishdID) '
         + ' value(:hdid,:goodsID,:qty,:salePrice,:actualPrice,:discount,:amount,:desc,:isin,:dishdID)';
  Result := False;
  DMMySQL.conMySQL.StartTransaction;
  try
    with cdsSaleDT do
    begin
      DisableControls;
      try
        First;
        while not Eof do
        begin
          DMMySQL.mycmdCom.SQL.Clear;
          DMMySQL.mycmdCom.SQL.Add(sqlStr);
          DMMySQL.mycmdCom.ParamByName('desc').AsString :=FieldByName('desc').AsString;
          DMMySQL.mycmdCom.ParamByName('hdid').AsInteger :=hdid;
          DMMySQL.mycmdCom.ParamByName('goodsID').AsInteger :=FieldByName('goodsID').AsInteger;
          DMMySQL.mycmdCom.ParamByName('qty').AsInteger :=FieldByName('qty').AsInteger;
          DMMySQL.mycmdCom.ParamByName('salePrice').AsFloat :=FieldByName('salePrice').AsFloat;
          DMMySQL.mycmdCom.ParamByName('actualPrice').AsFloat :=FieldByName('actualPrice').AsFloat;
          DMMySQL.mycmdCom.ParamByName('discount').AsFloat :=FieldByName('discount').AsFloat;
          DMMySQL.mycmdCom.ParamByName('amount').AsFloat :=FieldByName('amount').AsFloat;
          DMMySQL.mycmdCom.ParamByName('dishdID').AsInteger :=FieldByName('dishdID').AsInteger;
          DMMySQL.mycmdCom.ParamByName('isin').AsInteger :=isin;
          DMMySQL.mycmdCom.Execute;

          TStoreGoodsInfoDB.updateQty(FieldByName('goodsID').AsInteger,FieldByName('qty').AsInteger *-1);
          Next;
        end;
      finally
        EnableControls;
      end;
    end;
    with cdsDouDis do
    begin
      DisableControls;
      try
        sqlStr := ' insert into saledoudiscount(hdid,disPlanid,plan_type,plan_name,plan_Desc,total,discount,discountTotal,subMoney) '
         + ' value(:hdid,:disPlanid,:plan_type,:plan_name,:plan_Desc,:total,:discount,:discountTotal,:subMoney)';
        First;
        while not eof do
        begin
          DMMySQL.mycmdCom.SQL.Clear;
          DMMySQL.mycmdCom.SQL.Add(sqlStr);
          DMMySQL.mycmdCom.ParamByName('hdid').AsInteger :=hdid;
          DMMySQL.mycmdCom.ParamByName('disPlanid').AsInteger :=FieldByName('disPlanid').AsInteger;
          DMMySQL.mycmdCom.ParamByName('plan_type').AsString :=FieldByName('plan_type').AsString;
          DMMySQL.mycmdCom.ParamByName('plan_name').AsString :=FieldByName('plan_name').AsString;
          DMMySQL.mycmdCom.ParamByName('plan_Desc').AsString :=FieldByName('plan_Desc').AsString;
          DMMySQL.mycmdCom.ParamByName('total').AsFloat :=FieldByName('total').AsFloat;
          DMMySQL.mycmdCom.ParamByName('discount').AsFloat :=FieldByName('discount').AsFloat;
          DMMySQL.mycmdCom.ParamByName('discountTotal').AsFloat :=FieldByName('discountTotal').AsFloat;
          DMMySQL.mycmdCom.ParamByName('subMoney').AsFloat :=FieldByName('subMoney').AsFloat;
          DMMySQL.mycmdCom.Execute;
          Next;
        end;  
      finally
        EnableControls;
      end;
    end;
    DMMySQL.conMySQL.Commit;
    Result := true;
  except
    DMMySQL.conMySQL.Rollback;
  end;
end;

function TSaleObj.InsertSaleHD(salesmen, vipCode, date, saleNO, Storecode:
    string; totalQty: Integer; totalAmount, card, cash, creditCard,
    shoppingCard, otherCard, actualAmount: Double; isin: Integer; paymentdesc:
    string;errorMoney: Double): Integer;
var sqlStr :string;
begin
  Result := -1;
  sqlStr := ' insert into salehd(salesmen,vipCode,date,saleNO,storeCode,totalQty,totalAmount,card,cash,creditCard, shoppingCard, otherCard,actualAmount,isin,paymentdesc,errorMoney) '+
      ' value(:salesmen,:vipCode,:date,:saleNO,:storeCode,:totalQty,:totalAmount,:card,:cash,:creditCard, :shoppingCard, :otherCard,:actualAmount,:isin,:paymentdesc,:errorMoney)';
  with DMMySQL.mycmdCom do
  begin
    SQL.Clear;
    SQL.Add(sqlStr);
    ParamByName('salesmen').AsString :=salesmen;
    ParamByName('vipCode').AsString :=vipCode;
    ParamByName('date').AsString :=date;
    ParamByName('saleNO').AsString :=saleNO;
    ParamByName('storeCode').AsString :=storeCode;
    ParamByName('totalQty').AsInteger :=totalQty;
    ParamByName('totalAmount').AsFloat :=totalAmount;
    ParamByName('card').AsFloat :=card;
    ParamByName('cash').AsFloat :=cash;
    ParamByName('creditCard').AsFloat :=creditCard;
    ParamByName('shoppingCard').AsFloat :=shoppingCard;
    ParamByName('otherCard').AsFloat :=otherCard;
    ParamByName('actualAmount').AsFloat :=actualAmount;
    ParamByName('isin').AsInteger :=isin;
    ParamByName('paymentdesc').AsString :=paymentdesc;
    ParamByName('errorMoney').AsFloat :=errorMoney;
    Execute;
  end;
  with DMMySQL.myqryCom do
  begin
    Close;
    try
      sqlStr := ' select id from salehd where saleNO="'+saleNO+'" ';
      SQL.Clear;
      SQL.Add(sqlStr);
      Open;
      if(not IsEmpty) then
      begin
        Result := FieldByName('ID').AsInteger;
      end;
    finally
      Close;
    end;
  end;
end;

end.
